<template>
  <div class="container">
    <div>
      <el-row :gutter="12">
        <el-col :span="5">
          <el-form ref="form" :model="form">
            <el-form-item>
              <!-- <el-radio v-model="form.radioData" label="日">按日期：</el-radio> -->
              <span>查询日期：</span>
              <el-date-picker
                v-model="form.valueDay"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                style="width: 150px"
              ></el-date-picker>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="5">
          <el-form ref="form" :model="form">
            <el-form-item label="车辆名称：">
              <el-select
                v-model="form.cph"
                placeholder="请选择"
                filterable
                clearable
                style="width: 150px"
              >
                <el-option
                  v-for="item in form.cphList"
                  :key="item.index"
                  :label="item.车辆名称"
                  :value="item.车辆名称"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="5">
          <el-form ref="form" :model="form">
            <el-form-item label="车辆类型：">
              <el-select
                v-model="form.clx"
                placeholder="请选择"
                clearable
                style="width: 150px"
              >
                <el-option
                  v-for="(item, index) in clxList"
                  :value="item.label"
                  :label="item.value"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="5">
          <el-form ref="form" :model="form">
            <el-form-item label="道路名称：">
              <el-input
                v-model="form.roadName"
                placeholder="请输入道路名称"
                clearable
                style="width: 150px"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="5" v-show="this.CLIENT_NO === '00000'">
          <el-form ref="form" :model="form">
            <el-form-item label="分组：">
              <el-select
                v-model="CLIENT_GROUP_NAME"
                placeholder="请选择分组"
                style="width: 150px"
                @change="getSelectCarList()"
              >
                <el-option
                  v-for="(item, index) in groupingList"
                  :value="item.CLIENT_GROUP_NO"
                  :label="item.CLIENT_GROUP_NAME"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="4">
          <el-form ref="form" :model="form" style="float: left">
            <el-button size="mini" type="primary" @click="query"
              >查 询</el-button
            >
            <el-button size="mini" type="success" @click="exportExcel"
              >导 出</el-button
            >
          </el-form>
        </el-col>
      </el-row>
    </div>
    <el-table
      border
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      max-height="320"
      highlight-current-row
    >
      <el-table-column
        prop="车辆名称"
        label="车辆名称"
        align="center"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="车辆类型"
        label="车辆类型"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="道路名称"
        label="道路名称"
        align="center"
        width="130"
      ></el-table-column>
      <el-table-column prop="道路类型" label="道路类型"></el-table-column>
      <el-table-column
        prop="道路起点"
        label="道路起点"
        width="130"
      ></el-table-column>
      <el-table-column
        prop="道路终点"
        label="道路终点"
        width="130"
      ></el-table-column>
      <el-table-column prop="道路长度" label="道路长度（米）"></el-table-column>
      <el-table-column prop="作业长度" label="作业长度（米）"></el-table-column>
      <el-table-column prop="作业次数" label="作业次数"></el-table-column>
      <el-table-column prop="作业时间" label="作业时间"></el-table-column>
      <el-table-column
        prop="作业开始时间"
        label="作业开始时间"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="作业结束时间"
        label="作业结束时间"
        width="150"
      ></el-table-column>
      <!-- <el-table-column prop="SD1" label="夜班开始时间"></el-table-column>
      <el-table-column prop="ED1" label="夜班结束时间"></el-table-column>
      <el-table-column prop="SD2" label="早班开始时间"></el-table-column>
      <el-table-column prop="ED2" label="早班结束时间"></el-table-column> -->
      <!-- <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-info" @click="handleEdit(scope.$index, scope.row)">详情</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="tableData.length"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import '@/assets/css/common.css'
import { formatDate } from '@/common/js/date.js'
import export2Excel from '@/utils/exportExcel/index.js'
export default {
  data () {
    return {
      currentPage: 1, //初始页
      pageSize: 50,    //    每页的数据
      CLIENT_NO: localStorage.getItem("CLIENT_NO"),
      form: {
        cphList: [],
        cph: '',
        clx: '全部',
        roadName: "",
        radioData: '日',
        valueDay: formatDate(new Date(), 'yyyy-MM-dd'),
        valueMonth: ''
      },
      clxList: [{
        value: '全部',
        label: '全部'
      }, {
        value: '洒水车',
        label: '洒水车'
      }, {
        value: '高压清洗车',
        label: '高压清洗车'
      }],
      selectCarList: [],
      tableData: [],
      originalData: [],
      exportExcelData: [],
      groupingList: [],
      CLIENT_GROUP_NAME: ''
    }
  },
  created () {
    this.getGroupingList();
  },
  methods: {
    // 获得分组列表
    async getGroupingList () {
      var url =
        "Vehicle/GetClientInfo?client_no=" + localStorage.getItem("CLIENT_NO"); // zxdata
      let { data } = await this.$Http.get(url);
      this.groupingList = data.slice(0, 6);
      // this.groupingList.unshift({ "CLIENT_GROUP_NO": localStorage.getItem("CLIENT_NO"), "CLIENT_GROUP_NAME": "全部" })
      this.CLIENT_GROUP_NAME = this.groupingList[0].CLIENT_GROUP_NO;
      // this.getRoadList(data[0].CLIENT_GROUP_NO);
      this.query();
    },
    query () {
      this.getSelectCarList();
      this.getlistdata();
    },
    async getSelectCarList () {
      var date = ''
      if (this.form.radioData === '日') {
        date = this.form.valueDay
      }
      if (this.form.radioData === '月') {
        date = this.form.valueMonth
      }
      var url = 'Vehicle/GetVehicleWorkRoadCarSX?WORKDATE=' + date + '&CLIENT_NO=' + this.CLIENT_GROUP_NAME
      const { data } = await this.$Http.get(url)
      this.form.cphList = data

    },
    async getlistdata () {
      this.currentPage = 1;
      var date = ''
      if (this.form.radioData === '日') {
        date = this.form.valueDay
      }
      if (this.form.radioData === '月') {
        date = this.form.valueMonth
      }
      var url = 'Vehicle/GetVehicleWorkRoadSX?WORKDATE=' + date + '&CLIENT_NO=' + this.CLIENT_GROUP_NAME
      const { data } = await this.$Http.get(url)
      this.exportExcelData = data
      for (var i = 0; i < data.length;) {
        var item = data[i][1]
        if (data[i].CAR_TYPE === '车辆类型' || data[i].CAR_TYPE === '') {
          data.splice(i, 1);
        } else {
          data[i].timeStr = (data[i].WTime / 60).toFixed(0) + '分钟'
          i++;  //只有在没有删除元素时才对索引 i++
        }
      }
      this.tableData = data
      if (this.form.cph != '') this.tableData = this.tableData.filter(item => item.车辆名称.includes(this.form.cph))
      if (this.form.roadName != '') this.tableData = this.tableData.filter(item => item.道路名称.includes(this.form.roadName))
      if (this.form.clx !== '全部') this.tableData = this.tableData.filter(item => item.车辆类型 === this.form.clx)
      console.log(this.tableData)
    },
    // 导出Excel
    exportExcel () {
      let excelName = '车辆作业统计表'
      const tHeader = ['车辆名称', '车辆类型', '道路名称', '道路类型', '道路起点', '道路终点', '道路总长度（米）', '作业长度（米）', '作业次数', '作业时间', '作业开始时间', '作业结束时间']
      const filterVal = ['车辆名称', '车辆类型', '道路名称', '道路类型', '道路起点', '道路终点', '道路长度', '作业长度', '作业次数', '作业时间', '作业开始时间', '作业结束时间']
      export2Excel(tHeader, filterVal, this.exportExcelData, excelName)
    },
    //详情
    handleEdit (index, row) {
      //   this.idx = index;
      //   this.form = row;
      //   this.editVisible = true;
    },
    // 分页导航
    handlePageChange (currentPage) {
      this.currentPage = currentPage;
      // console.log(this.currentPage)
      // this.$set(this.query, 'pageIndex', val);
      // this.getData();
    },
    handleSizeChange (size) {
      this.pageSize = size;
      console.log(this.pageSize)
    }
  }
}
</script>
<style>
.content {
  overflow-y: scroll;
}
.el-col-6 {
  min-width: 400px;
  min-height: 50px;
}
.el-table th {
  height: 40px;
}
</style>